<?php
 

define('IN_SCRIPT',1);

/* Get all the required files and functions */
require_once('hd_settings.inc.php');
require_once('language/'.$hd_settings['language'].'.inc.php');
require_once('inc/common.inc.php');
hd_session_start();
hd_isLoggedIn();
require_once('inc/database.inc.php');
hd_dbConnect() or hd_error("$hdlang[cant_connect_db] $hdlang[contact_webmsater] $hd_settings[webmaster_mail]!");

/* Print header */
require_once('inc/header.inc.php');

$sql = "SELECT * FROM `hd_requests` WHERE ";

if ($_GET['archive']) {$archive=1;$sql .= "`archive`='1' AND ";}
else {$archive=0;}

$sql .= hd_myResidences();
$sql .= " AND ";

/* Get all the SQL sorting preferences */
$what=hd_input($_GET['what'],"$hdlang[int_error]: no what defined");

switch ($what) {
case "requestid":
    $extra=hd_input($_GET['requestid'],$hdlang['enter_id']);
    $sql .= "`requestid` = '$extra' ";
    break;
case "name":
    $extra=hd_input($_GET['name'],$hdlang['enter_name']);
    $sql .= "`name` LIKE '%$extra%' ";
    break;
case "dt":
    $extra=hd_input($_GET['dt'],$hdlang['enter_date']);
        if (!preg_match("/\d{4}-\d{2}-\d{2}/",$extra))
        {
            hd_error($hdlang['date_not_valid']);
        }
    $sql .= "`dt` LIKE '$extra%' ";
    break;
case "subject":
    $extra=hd_input($_GET['subject'],$hdlang['enter_subject']);
    $sql .= "`subject` LIKE '%$extra%' ";
    break;
default:
    hd_error($hdlang['invalid_search']);
}

/* Print admin navigation */
require_once('inc/show_admin_nav.inc.php');

?>

</td>
</tr>
<tr>
<td>

<h3 align="center"><?php echo $hdlang['requests_found']; ?></h3>

<?php
$maxresults = hd_isNumber($_GET['limit']) or $maxresults=$hd_settings['max_listings'];
$thispage = hd_isNumber($_GET['page']) or $thispage=1;

$sort = hd_input($_GET['sort']) or $sort='priority';
$sql .= " ORDER BY `$sort` ";

if (isset($_GET['asc']) && $_GET['asc']==0) {$sql .= " DESC ";$asc=0;$asc_rev=1;}
else {$sql .= " ASC ";$asc=1;$asc_rev=0;}

$query = "what=$what&requestid=$extra&name=$extra&date=$extra&subject=$extra&limit=$maxresults&archive=$archive&asc=$asc&sort=$sort&page=";

/* Get number of requests and page number */
$result = hd_dbQuery($sql) or hd_error("$hdlang[cant_sql]: $sql</p><p>$hdlang[mysql_said]:<br>".mysql_error()."</p><p>$hdlang[contact_webmsater] $hd_settings[webmaster_mail]");
$all = hd_dbNumRows($result);

$thispages = ceil($all/$maxresults) or $thispages=1;
if ($thispage > $thispages) {$thispage=$thispages;}
$limit_down = (($thispage*$maxresults)-$maxresults);

    $prev_page = ($thispage-1 <= 0) ? 0 : $thispage-1;
    $next_page = ($thispage+1 > $thispages) ? 0 : $thispage+1;


    echo '<form name="HPage"><p align="center">'.sprintf($hdlang['requests_on_pages'],$all,$thispages).' <select name="myHpage">';
    for ($i=1;$i<=$thispages;$i++) {
        echo '<option value="'.$i.'">'.$i.'</option>';
    }
    echo'</select> <input type="button" value="'.$hdlang['go'].'" onClick="Javascript:window.location=\'show_requests.php?'.$query.'\'+document.HPage.myHpage.value"><br />';

    if ($thispages > 7 && $prev_page) {
        echo '
        <a href="find_requests.php?'.$query.$i.'1">&lt;&lt;</a>
        &nbsp;
        <a href="find_requests.php?'.$query.$prev_page.'">&lt;</a>
        &nbsp;
        ';
    }

    for ($i=1; $i<=$thispages; $i++) {
        if ($i <= ($thispage+5) && $i >= ($thispage-5)) {
           if($i == $thispage) {echo ' <b>'.$i.'</b> ';}
           else {echo ' <a href="find_requests.php?'.$query.$i.'">'.$i.'</a> ';}
        }
    }

    if ($thispages > 7 && $next_page) {
        echo '
        &nbsp;
        <a href="find_requests.php?'.$query.$next_page.'">&gt;</a>
        &nbsp;
        <a href="find_requests.php?'.$query.$thispages.'">&gt;&gt;</a>
        ';
    }

    echo '</p></form>';

/* We have the full SQL query now, get requests */
$sql .= " LIMIT $limit_down,$maxresults ";
$result = hd_dbQuery($sql) or hd_error("$hdlang[cant_sql]: $sql</p><p>$hdlang[mysql_said]:<br>".mysql_error()."</p><p>$hdlang[contact_webmsater] $hd_settings[webmaster_mail]");

$query = "what=$what&requestid=$extra&name=$extra&date=$extra&subject=$extra&limit=$maxresults&archive=$archive&page=$thispage&asc=$asc_rev&sort=";

/* Print the table with requests */
$random=rand(10000,99999);
?>

<form name="form1" action="delete_requests.php" method="POST">

<div align="center">
<center>
<table border="0" width="750" cellspacing="1" cellpadding="3" class="white">
<tr>
<td class="admin_white">&nbsp;</td>
<td class="admin_white"><a href="find_requests.php?<?php echo $query; ?>requestid"><?php echo $hdlang['trackID']; ?></a></td>
<td class="admin_white" align="center"><a href="find_requests.php?<?php echo $query; ?>lastchange"><?php echo $hdlang['last_update']; ?></a></td>
<td class="admin_white"><a href="find_requests.php?<?php echo $query; ?>name"><?php echo $hdlang['name']; ?></a></td>
<td class="admin_white"><a href="find_requests.php?<?php echo $query; ?>subject"><?php echo $hdlang['subject']; ?></a></td>
<td class="admin_white" align="center"><a href="find_requests.php?<?php echo $query; ?>status"><?php echo $hdlang['status']; ?></a></td>
<td class="admin_white" align="center"><a href="find_requests.php?<?php echo $query; ?>lastreplier"><?php echo $hdlang['last_replier']; ?></a></td>
<td class="admin_white" align="center"><a href="find_requests.php?<?php echo $query; ?>priority"><?php echo $hdlang['priority']; ?></a></td>
<td class="admin_white" align="center"><a href="find_requests.php?<?php echo $query; ?>archive"><?php echo $hdlang['archived']; ?></a></td>
</tr>

<?php
while ($request=hd_dbFetchAssoc($result))
{
    if ($i) {$color="admin_gray"; $i=0;}
    else {$color="admin_white"; $i=1;}

    switch ($request['status']) {
    case 0:
        $request['status']='<font class="open">'.$hdlang['open'].'</font>';
        break;
    case 1:
        $request['status']='<font class="waitingreply">'.$hdlang['wait_reply'].'</font>';
        break;
    case 2:
        $request['status']='<font class="replied">'.$hdlang['replied'].'</font>';
        break;
    default:
        $request['status']='<font class="resolved">'.$hdlang['closed'].'</font>';
    }

    switch ($request['priority']) {
    case 1:
        $request['priority']='<font class="important">'.$hdlang['high'].'</font>';
        break;
    case 2:
        $request['priority']='<font class="medium">'.$hdlang['medium'].'</font>';
        break;
    default:
        $request['priority']=$hdlang['low'];
    }

    $request['lastchange']=hd_formatDate($request['lastchange']);

    if ($request['lastreplier']=='1') {$request['lastreplier']=$hdlang['staff'];}
    else {$request['lastreplier']=$hdlang['Student'];}

    if ($request['archive']) {$request['archive']=$hdlang['yes'];}
    else {$request['archive']=$hdlang['no'];}

    if ($request['attachments']) {$request['attachments']='<img src="img/clip.gif" width="20" height="20" alt="'.$hdlang['clip_alt'].'">';}

echo <<<EOC
<tr>
<td class="$color" align="center"><input type="checkbox" name="id[]" value="$request[id]"></td>
<td class="$color"><a href="admin_request.php?track=$request[requestid]&Refresh=$random">$request[requestid]</a></td>
<td class="$color" align="center">$request[lastchange]</td>
<td class="$color">$request[name]</td>
<td class="$color">$request[attachments]<a href="admin_request.php?track=$request[requestid]&Refresh=$random">$request[subject]</a></td>
<td class="$color" align="center">$request[status]</td>
<td class="$color" align="center">$request[lastreplier]</td>
<td class="$color" align="center">$request[priority]</td>
<td class="$color" align="center">$request[archive]</td>
</tr>

EOC;
} // End while
?>
</table>
</center>
</div>

<p align="center"><select name="a">
<option value="delete" selected><?php echo $hdlang['del_selected']; ?></option>
<option value="close"><?php echo $hdlang['close_selected']; ?></option>
</select>
<input type="submit" value="<?php echo $hdlang['execute']; ?>" class="button"></p>

</form>

<hr>

<?php
require_once('inc/show_search_form.inc.php');
?>


<hr width="750">
<p>&nbsp;</p>
<?php

/* Print footer */
require_once('inc/footer.inc.php');
exit();

?>
